{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Specifying_batch_size_while_training_a_model.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.6"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/PacktPublishing/Modern-Computer-Vision-with-PyTorch/blob/master/Chapter02/Specifying_batch_size_while_training_a_model.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:09.313991Z",
          "start_time": "2020-09-25T19:38:09.019280Z"
        },
        "id": "qMRMqX5bxknN"
      },
      "source": [
        "from torch.utils.data import Dataset, DataLoader\n",
        "import torch\n",
        "import torch.nn as nn"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:09.317494Z",
          "start_time": "2020-09-25T19:38:09.315108Z"
        },
        "id": "pwlhqEFtxldo"
      },
      "source": [
        "x = [[1,2],[3,4],[5,6],[7,8]]\n",
        "y = [[3],[7],[11],[15]]"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:09.322276Z",
          "start_time": "2020-09-25T19:38:09.319108Z"
        },
        "id": "Xvn8m82PxqGL"
      },
      "source": [
        "X = torch.tensor(x).float()\n",
        "Y = torch.tensor(y).float()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:10.908620Z",
          "start_time": "2020-09-25T19:38:09.324375Z"
        },
        "id": "suZ9rXQqxrUi"
      },
      "source": [
        "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
        "X = X.to(device)\n",
        "Y = Y.to(device)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:10.913405Z",
          "start_time": "2020-09-25T19:38:10.909638Z"
        },
        "id": "GZwy83Zaxsrw",
        "outputId": "cb09c682-1509-420d-d663-91dff0f00e91",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 105
        }
      },
      "source": [
        "class MyDataset(Dataset):\n",
        "    def __init__(self,x,y):\n",
        "        self.x = torch.tensor(x).float()\n",
        "        self.y = torch.tensor(y).float()\n",
        "    def __len__(self):\n",
        "        return len(self.x)\n",
        "    def __getitem__(self, ix):\n",
        "        return self.x[ix], self.y[ix]\n",
        "ds = MyDataset(X, Y)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/home/yyr/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:3: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
            "  This is separate from the ipykernel package so we can avoid doing imports until\n",
            "/home/yyr/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:4: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
            "  after removing the cwd from sys.path.\n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:10.917418Z",
          "start_time": "2020-09-25T19:38:10.915031Z"
        },
        "id": "K28I9mj-x1yB"
      },
      "source": [
        "dl = DataLoader(ds, batch_size=2, shuffle=True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:10.922176Z",
          "start_time": "2020-09-25T19:38:10.918671Z"
        },
        "id": "i1uNDQLDx3bb"
      },
      "source": [
        "class MyNeuralNet(nn.Module):\n",
        "    def __init__(self):\n",
        "        super().__init__()\n",
        "        self.input_to_hidden_layer = nn.Linear(2,8)\n",
        "        self.hidden_layer_activation = nn.ReLU()\n",
        "        self.hidden_to_output_layer = nn.Linear(8,1)\n",
        "    def forward(self, x):\n",
        "        x = self.input_to_hidden_layer(x)\n",
        "        x = self.hidden_layer_activation(x)\n",
        "        x = self.hidden_to_output_layer(x)\n",
        "        return x"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:10.928421Z",
          "start_time": "2020-09-25T19:38:10.924741Z"
        },
        "id": "M5GAnaPmx5w5"
      },
      "source": [
        "mynet = MyNeuralNet().to(device)\n",
        "loss_func = nn.MSELoss()\n",
        "from torch.optim import SGD\n",
        "opt = SGD(mynet.parameters(), lr = 0.001)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:11.011394Z",
          "start_time": "2020-09-25T19:38:10.929771Z"
        },
        "id": "_da8xi-9x7oJ",
        "outputId": "7a59135d-f162-4178-f05e-2e5492893216",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "import time\n",
        "loss_history = []\n",
        "start = time.time()\n",
        "for _ in range(50):\n",
        "    for data in dl:\n",
        "        x, y = data\n",
        "        opt.zero_grad()\n",
        "        loss_value = loss_func(mynet(x),y)\n",
        "        loss_value.backward()\n",
        "        opt.step()\n",
        "        loss_history.append(loss_value)\n",
        "end = time.time()\n",
        "print(end - start)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.07826399803161621\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:11.014114Z",
          "start_time": "2020-09-25T19:38:11.012332Z"
        },
        "id": "ou1Hdxb4x9TP"
      },
      "source": [
        "val_x = [[10,11]]"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:11.017321Z",
          "start_time": "2020-09-25T19:38:11.015262Z"
        },
        "id": "OtA5Oswrx_Sl"
      },
      "source": [
        "val_x = torch.tensor(val_x).float().to(device)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "ExecuteTime": {
          "end_time": "2020-09-25T19:38:11.025594Z",
          "start_time": "2020-09-25T19:38:11.018406Z"
        },
        "id": "UysdiiFSyAf1",
        "outputId": "f11228a4-0aaa-4851-eee2-a855a93acd91",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        }
      },
      "source": [
        "mynet(val_x)"
      ],
      "execution_count": null,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[20.7331]], device='cuda:0', grad_fn=<AddmmBackward>)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rHYynD1wyByW"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}